//
//  AIRegisterController.m
//  MiaMind
//
//  Created by Vill on 2023/12/11.
//

#import "AIRegisterController.h"
#import "AIChooseSoulmateView.h"     // 选择人物模型
#import "AIInfoSetView.h"            // 修改人物信息

@interface AIRegisterController ()

@property (nonatomic, strong) AIChooseSoulmateView *mateView;
@property (nonatomic, strong) AIInfoSetView *infoSetView;

@property (nonatomic, strong) UIView *containView;

@end

@implementation AIRegisterController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [AIUnityInstance.instance pause:NO];
    [AIUnityInstance.instance showUnityView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [AIUnityInstance.instance pause:YES];
    [AIUnityInstance.instance showNativeView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [AIUnityInstance.instance initUnityWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [AIUnityInstance.instance.unityAppController.rootView addSubview:self.mateView];
    });
}

#pragma mark ---------------- lazy ------------------
// 模型View
- (AIChooseSoulmateView *)mateView{
    if(!_mateView){
        _mateView = [[AIChooseSoulmateView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        WeakSelf(self);
        _mateView.itemClickBlock = ^(AISoulMateModel * _Nonnull model, NSInteger index) {
            [weakself chooseItem:model index:index];
        };
        _mateView.nextClickBlock = ^{
            // 近距离
            [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
            [weakself creatInfoView];
            weakself.mateView = nil;
        };
    }
    return _mateView;
}

// 设置信息View
- (AIInfoSetView *)infoSetView{
    if(!_infoSetView){
        _infoSetView = [[AIInfoSetView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        WeakSelf(self);
        // 关闭页面
        _infoSetView.closeClickBlock = ^{
            [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
            [UIView animateWithDuration:0.3 animations:^{
                weakself.mateView.alpha = 1;
            } completion:^(BOOL finished) {
                [AIUnityInstance.instance.unityAppController.rootView addSubview:weakself.mateView];
            }];
        };
    }
    return _infoSetView;
}

- (UIView *)containView{
    if(!_containView){
        _containView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, KScreenWidth-80, KScreenHeight-200)];
        _containView.backgroundColor = UIColor.redColor;
    }
    return _containView;
}

#pragma mark --------------- chooseitem ----------------
- (void)chooseItem:(AISoulMateModel *)model index:(NSInteger)index{
    NSString *indexStr = [NSString stringWithFormat:@"%ld",(long)index];
//    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"PlayerTalk" UTF8String] message:[@"你好" UTF8String]];
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"SelectPlayer" UTF8String] message:[indexStr UTF8String]];
}

// 拉起修改个人信息
-(void)creatInfoView{
    [UIView animateWithDuration:0.3 animations:^{
        self.infoSetView.alpha = 1;
    } completion:^(BOOL finished) {
        [AIUnityInstance.instance.unityAppController.rootView addSubview:self.infoSetView];
    }];
}

@end


